package com.example.administrator.daydayappmarket.recommend.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.example.administrator.daydayappmarket.Contacts;
import com.example.administrator.daydayappmarket.DownloadActivity;
import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.SearchActivity;
import com.example.administrator.daydayappmarket.recommend.adapter.RecommendMainFootListViewAdapter;
import com.example.administrator.daydayappmarket.recommend.moduls.RecommendApps;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarException;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class RecommendTopImageSpecialThirdActivity extends AppCompatActivity {

    @InjectView(R.id.activity_recommend_top_image_special_third_imageback)
    RelativeLayout imageback;
    @InjectView(R.id.activity_recommend_top_image_special_third_imagedownload)
    RelativeLayout imagedownload;
    @InjectView(R.id.activity_recommend_top_image_special_third_imagesearch)
    RelativeLayout imagesearch;
    @InjectView(R.id.activity_recommend_top_image_special_third_listView)
    ListView listView;
    private String id;
    private RequestQueue requestQueue;
    private StringRequest stringRequest;
    private List<RecommendApps> appses;
    private RecommendMainFootListViewAdapter adapter;
    private View inflate;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_recommend_top_image_special_third);
        ButterKnife.inject(this);

        Intent intent = getIntent();
        id = intent.getStringExtra("id");

        init();//初始化

    }

    private void init() {
        requestQueue = Volley.newRequestQueue(this);
        appses = new ArrayList<>();
        adapter = new RecommendMainFootListViewAdapter(appses,this);
        View view = LayoutInflater.from(this).inflate(R.layout.layout_listview_empty,null);
        Glide.with(this).load(R.drawable.cat_loading_transparent).into((ImageView) view.findViewById(R.id.layout_listView_empty_image));
        listView.setEmptyView(view);
        inflate = LayoutInflater.from(RecommendTopImageSpecialThirdActivity.this)
                .inflate(R.layout.layout_recommend_top_image_special_third_head, null);
        listView.addHeaderView(inflate);
//
        listView.setAdapter(adapter);

        stringRequest = new StringRequest(Contacts.Tab_TOPICSPECIALTHIRD + id, new Response.Listener<String>() {



            @Override
            public void onResponse(String response) {

                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONObject data1 = jsonObject.getJSONObject("data");
                    String banner = data1.getString("banner");
                    String lead_content = data1.getString("lead_content");
                    String title = data1.getString("title");

                    if(banner.startsWith("http")){
                        Glide.with(RecommendTopImageSpecialThirdActivity.this)
                                .load(banner).into((ImageView) inflate.findViewById(R.id.item_recommend_top_image_special_third_head_image));
                    }else {
                        Glide.with(RecommendTopImageSpecialThirdActivity.this)
                                .load(Contacts.URL_BASEIMAGE+banner).into((ImageView) inflate.findViewById(R.id.item_recommend_top_image_special_third_head_image));
                    }
                    TextView textView = (TextView) inflate.findViewById(R.id.item_recommend_top_image_special_third_head_text_title);
                    TextView textView1 = (TextView) inflate.findViewById(R.id.item_recommend_top_image_special_third_head_text_subject);
                    textView.setText(title);
                    textView1.setText(lead_content);



                    JSONArray apps1 = data1.getJSONArray("apps");

                    for (int i = 0; i < apps1.length(); i++) {
                        JSONObject jsonObject1 = apps1.getJSONObject(i);
                        RecommendApps apps = new RecommendApps();
                        apps.JsonParse(jsonObject1);
                        appses.add(apps);
                    }

                    adapter.notifyDataSetChanged();


                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });


        stringRequest.setTag("specialthirdrequest");
        requestQueue.add(stringRequest);
        requestQueue.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        requestQueue.cancelAll("specialthirdrequest");
    }

    @OnClick({R.id.activity_recommend_top_image_special_third_imageback, R.id.activity_recommend_top_image_special_third_imagedownload, R.id.activity_recommend_top_image_special_third_imagesearch})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.activity_recommend_top_image_special_third_imageback:
                finish();
                break;
            case R.id.activity_recommend_top_image_special_third_imagedownload:
                Intent intent = new Intent(this, DownloadActivity.class);
                startActivity(intent);
                break;
            case R.id.activity_recommend_top_image_special_third_imagesearch:
                Intent intent1 = new Intent(this, SearchActivity.class);
                startActivity(intent1);
                break;

        }
    }
}
